﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BlogKickStart.Models;
using MarkdownSharp;

namespace BlogKickStart.Controllers
{
    public class BlogPostController : Controller
    {
		private int pageSize = 5;
		
        //
        // GET: /BlogPost/
		public ActionResult Index(int? page)
		{
			using (BlogKickstartContext data = new BlogKickstartContext())
			{ ViewBag.themeName = (from p in data.Blogs where p.BlogID == 1 select p.Style.StyleNM).Single(); }


			using (var data = new BlogKickstartContext())
			{
				var pagedPosts = data.Posts.Include("Comments").OrderBy(Post => Post.Date).Skip((page ?? 0) * pageSize).Take(pageSize).ToList();
				ViewBag.page = page ?? 0;
				
				ViewBag.pages = Math.Floor((decimal)(data.Posts.Count() / pageSize));
				return View(pagedPosts);
			}
			//return View();
		}

		[HttpPost]
		public ActionResult Index(Post blogpost)
		{
			using (BlogKickstartContext data = new BlogKickstartContext())
			{ ViewBag.themeName = (from p in data.Blogs where p.BlogID == 1 select p.Style.StyleNM).Single(); }

			if (ModelState.IsValid)
			{
				using (var data = new BlogKickstartContext())
				{
					data.Posts.Add(blogpost);
					data.SaveChanges();
				}
				return RedirectToAction("Index");
			}

			return View(blogpost);
		}
				
		public ActionResult EditPost(int? postID, string unMarked, string Title, bool? delete)
		{
			using (BlogKickstartContext data = new BlogKickstartContext())
			{ ViewBag.themeName = (from p in data.Blogs where p.BlogID == 1 select p.Style.StyleNM).Single(); }

			using (var data = new BlogKickstartContext())
			{
				if (!postID.HasValue)
				{
					var newpost = data.Posts.Create();
					newpost.UnmarkedContent = " ";
					newpost.Title = " ";
					newpost.MarkedContent = " ";
					newpost.BlogID = 1;
					newpost.Date = System.DateTime.Now;
					data.Posts.Add(newpost);
					data.SaveChanges();
					return View(newpost);

				}	
				
				if (string.IsNullOrEmpty(unMarked) && string.IsNullOrEmpty(Title) && !delete.HasValue)
				{
					var post = data.Posts.Single(i => i.PostID == postID);
					return View(post);
					

				}
				else if(delete.HasValue)
				{
					if (delete.Value)
					{
						data.Posts.Remove(data.Posts.Single(i => i.PostID == postID));
						data.SaveChanges();
						return View("Index");
					}
					return View();
				}
				else
				{
					var post = data.Posts.Single(i => i.PostID == postID);
					var markupdown = new Markdown();
					post.MarkedContent = markupdown.Transform(unMarked);
					post.UnmarkedContent = unMarked;
					post.Title = Title;
					data.SaveChanges();

					return RedirectToAction("Index");
				}
			}
		}
		public ActionResult Comment(int PostID, string unMarked)
		{


			using (var data = new BlogKickstartContext())
			{
				ViewBag.themeName = (from p in data.Blogs where p.BlogID == 1 select p.Style.StyleNM).Single();


				if(!string.IsNullOrEmpty(unMarked))
				{

					var comment = data.Comments.Create();
					comment.PostID = PostID;
			
					var markupdown = new Markdown();
					comment.UserID = 1;
					comment.MarkedContent = markupdown.Transform(unMarked);
					comment.UnmarkedContent = unMarked;
					comment.CommentDate = System.DateTime.Now;
					data.Comments.Add(comment);
					data.SaveChanges();

					return RedirectToAction("Index");
				}
				return View();
			}

		}

    }
}
